package com.wss.module.wan.ui.main.adapter;

import com.felipecsl.asymmetricgridview.AgvRecyclerViewAdapter;
import com.felipecsl.asymmetricgridview.AsymmetricItem;
import com.felipecsl.asymmetricgridview.BaseViewHolder;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.bean.Template;
import com.wss.module.wan.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class HomeMenuAdapter extends AgvRecyclerViewAdapter<HomeMenuAdapter.HomeMenuViewHolder> {

    private Context context;
    private List<Template> list;
    private OnListItemClickListener<Template> listener;

    /**
     * 常规列表重写该方法
     *
     * @param context     context
     * @param list        数据源
     * @param listener    Item点击回调
     */
    public HomeMenuAdapter(Context context, List<Template> list, OnListItemClickListener<Template> listener) {
        this.context = context;
        this.list = list;
        this.listener = listener;
    }

    public void addAll(List<Template> data) {
        if (data == null) {
            return;
        }
        if (list == null) {
            list = new ArrayList<>();
        }
        list.clear();
        list.addAll(data);
        notifyDataChanged();
    }

    public void clear() {
        if (list != null) {
            list.clear();
        }
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public AsymmetricItem getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public HomeMenuViewHolder onCreateViewHolder(ComponentContainer componentContainer, int i) {
        return new HomeMenuViewHolder(context);
    }

    @Override
    public void onBindViewHolder(HomeMenuViewHolder homeMenuViewHolder, int i) {
        homeMenuViewHolder.bind(list.get(i), i);
    }

    class HomeMenuViewHolder extends BaseViewHolder {

        Text tvText;
        Image ivIcon;

        public HomeMenuViewHolder(Context context) {
            super(LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_wan_item_of_block_list,
                            null,
                            false));
            tvText = (Text) getComponent().findComponentById(ResourceTable.Id_tv_text);
            ivIcon = (Image) getComponent().findComponentById(ResourceTable.Id_iv_icon);
        }

        public void bind(Template data, int position) {

            tvText.setText(data.getTitle());

            try {
                ivIcon.setBackground(new PixelMapElement(context
                        .getResourceManager()
                        .getResource(data.getRes())));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NotExistException e) {
                e.printStackTrace();
            }

            getComponent().setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    listener.onItemClick(data, position);
                }
            });
        }
    }
}
